package com.MartynovichIgor.KnowledgeTest.tools.usertools;

import com.MartynovichIgor.KnowledgeTest.questions.Question;
import com.MartynovichIgor.KnowledgeTest.questions.QuestionFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.ResourceBundle;

import static java.lang.String.format;


/**
 * Created by:
 * User: Igor Martynovich and Guzenko Igor
 * Date: 25.10.13
 */
public class History {
    private ArrayList<Question> userQuestions;
    private ArrayList<Integer> userAnswersPoints;
    private ArrayList<Integer> shuffledIndexes;
    private int currentQuestion;

    public History(ArrayList<Integer> shuffledIndexes, ArrayList<Integer> loadedAnswers,
                   ResourceBundle languageBundle, int currentQuestion) throws IOException {
        this.userQuestions = new ArrayList<Question>();
        initializeField(userQuestions);
        this.userAnswersPoints = loadedAnswers;
        this.shuffledIndexes = shuffledIndexes;
        this.currentQuestion = currentQuestion;
        fillUserQuestions(new Loader().loadedQuestions(languageBundle));
    }

    /**
     * Метод записывает балы за ответ пользователя в заданый элемент
     * массива ответов.
     * @param userAnswer
     * @param currentPosition
     */
    public void setUserAnswer(String userAnswer, int currentPosition) {
        userAnswersPoints.set(currentPosition,userQuestions.get(currentPosition).getUserPointForQuestion(userAnswer));
    }

    public int getUserAnswer(int currentPosition) {
        return userAnswersPoints.get(currentPosition);
    }

    public int getCurrentQuestion() {
        return currentQuestion;
    }

    /**
     * Метод сохраняет пользователя.
     * @param login
     * @param password
     * @throws IOException
     */
    public void save(String login, String password, int userPoints) throws IOException {
        String answersInfo = createIndexesInfo(userAnswersPoints);
        String indexesInfo = createIndexesInfo(shuffledIndexes);
        String userInfo = format("%s %s %s %s", login, password, indexesInfo, answersInfo);
        new UserSaver().saveUserInfo(userInfo,login,userPoints);

    }

    /**
     * Метод печатает вопрос и усдовие(или варианты ответа) для заданого вопроса.
     * @param index
     */
    public void printQuestion(int index) {
        System.out.printf("%d)", index + 1);
        userQuestions.get(index).printQuestion();
        userQuestions.get(index).printCondition();
    }

    /**
     * Метод заполняет массив вопросов по масиву индексов.
     * @param questionLines
     */
    private void fillUserQuestions(ArrayList<String> questionLines) {
        QuestionFactory factory = new QuestionFactory();
        for (String questionLine : questionLines) {
            Question question = factory.getQuestion(questionLine);
            int questionId = question.getQuestionId();
            for (int indexPos = 0; indexPos < shuffledIndexes.size(); indexPos++) {
                if (shuffledIndexes.get(indexPos) == questionId)
                    userQuestions.set(indexPos, question);
            }
        }
    }

    private void initializeField(ArrayList field) {
        for (int i = 0; i <10 ; i++) {
            field.add(i,null);
        }
    }

    private String createIndexesInfo(ArrayList<Integer> array) {
        String line = "";
        for (int index : array) {
            line += format("%d/", index);
        }
        return line;
    }


}
